<?php
////////////////////////////////////////////////////////////////////////////
//                                                                        //
// NOTICE OF COPYRIGHT                                                    //
//                                                                        //
// Dean`s Office for Moodle                                               //
// Электронный деканат                                                    //
// <http://deansoffice.ru/>                                               //
//                                                                        //
// Copyright (C) 2012-2999  Kirill Krasnoschekov (Кирилл Краснощёков)     //
//                                                                        //
// This program is free software: you can redistribute it and/or modify   //
// it under the terms of the GNU General Public License as published by   //
// the Free Software Foundation, either version 3 of the Licensen.        //
//                                                                        //
// This program is distributed in the hope that it will be useful,        //
// but WITHOUT ANY WARRANTY; without even the implied warranty of         //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          //
// GNU General Public License for more details.                           //
//                                                                        //
// You should have received a copy of the GNU General Public License      //
// along with this program.  If not, see <http://www.gnu.org/licenses/>.  //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

require_once(dirname(realpath(__FILE__)).'/lib.php');
require_once(dirname(realpath(__FILE__)).'/common.php');



/**
 * Implementation for Free Dean's Office db interface
 * 
 * Needs $DOF to be global variable linking to DOF controller.
 * 
 * @author Kirill Krasnoschekov
 */
class DOFGuestbook extends Guestbook
{
   
    /**
     * Constructor
     * 
     * @author Kirill Krasnoschekov 
     */
    public function __construct() {}
    
    /**
     * Destructor: empty
     * 
     * @author Kirill Krasnoschekov 
     */
    public function __destruct() {}
    
    /**
     * Number of pages
     * 
     * @author Kirill Krasnoschekov 
     */
    public function CountPages()
    {
        global $DOF;
        
        $messages_number = $DOF->storage('gbmessages')->count();
        return ceil($messages_number / $this->messagesPerPage);
    }

    /**
     * Fetch messages on the given page
     * 
     * Pages are indexed from 0 to ($this->CountPages - 1).
     * Message: dictionary (id, name, text, timestamp)
     *
     * @param int $page 
     * Number of the page to fetch
     * @return array
     * List of messages fetched
     * @author Kirill Krasnoschekov 
     */    
    public function FetchMessages($page)
    {
        global $DOF;
        
        // Calculating message numbers on the given page
        $numStart = $page * $this->messagesPerPage;
        $numEnd = $numStart + $this->messagesPerPage;
        
        // Querying
        $records = $DOF->storage('gbmessages')->get_records(null, 
                'timestamp DESC', '*', $numStart, $this->messagesPerPage);
        // Converting records from objects into hashed arrays
        $recordsAssoc = array();
        foreach ( $records as $id => $record )
        {
            $recordsAssoc[] = array(
                    'id' => $id, 
                    'name' => $record->name,
                    'text' => $record->text,
                    'timestamp' => date('Y-m-d H:i:s', $record->timestamp));
        }
        return $recordsAssoc;
    }
    
    /**
     * Fetch comments to the given message
     * 
     * comment: dictionary (id, msgid, name, text, timestamp)
     * 
     * @param int $msgid
     * Message id for which comments are fetched
     * @return array
     * List of comments fetched
     * @author Kirill Krasnoschekov 
     */
    public function FetchComments($msgid) 
    {
        global $DOF;
        
        // Querying
        $records = $DOF->storage('gbcomments')->get_records(array('msgid' => $msgid));
        
        // Converting records from objects into hashed arrays
        $recordsAssoc = array();
        foreach ( $records as $id => $record )
        {
            $recordsAssoc[] = array(
                    'id' => $id,
                    'msgid' => $msgid, 
                    'name' => $record->name,
                    'text' => $record->text,
                    'timestamp' => date('Y-m-d H:i:s', $record->timestamp));
        }
        return $recordsAssoc;
    }
    
    /**
     * Add message
     * 
     * @param $name User name
     * @param $text Message text 
     * @author Kirill Krasnoschekov 
     */
    public function AddMessage($name, $text)
    {
        global $DOF;
        
        // Forming the object
        $message = new stdClass;
        $message->name = $name;
        $message->text = $text;
        $message->timestamp = time();
        // Querying the database
        return $DOF->storage('gbmessages')->insert($message);
    }
    
    /**
     * Add comment
     * 
     * @param integer $msgid Message id
     * @param string $name User name
     * @param string $text Comment text
     * @author Kirill Krasnoschekov 
     */
    public function AddComment($msgid, $name, $text)
    {
        global $DOF;
        
        // Checking if message does not exist
        if ( !$this->MessageExists($msgid) )
        {
            die(self::$err['MSGID_NOT_FOUND']);
        }
        // Forming the object
        $comment = new stdClass;
        $comment->name = $name;
        $comment->text = $text;
        $comment->msgid = $msgid;
        $comment->timestamp = time();
        // Querying
        return $DOF->storage('gbcomments')->insert($comment);
    }
    
    /**
     * Delete message
     * 
     * @param int $msg_id ID of the message to delete
     * @author Kirill Krasnoschekov 
     */
    public function DeleteMessage($msgid) 
    {
        global $DOF;
        
        // Comments for this message
        $comments = $DOF->storage('gbcomments')->get_records(array('msgid' => $msgid));
        foreach ($comments as $comment)
        {
            $DOF->storage('gbcomments')->delete($comment->id);
        }
        
        // Message
        return $DOF->storage('gbmessages')->delete($msgid);
    }
    
    /**
     * Delete comment 
     * 
     * @param int $commentid ID of the comment to delete
     * @author Kirill Krasnoschekov 
     */
    public function DeleteComment($commentid) 
    {
        global $DOF;
        
        return $DOF->storage('gbcomments')->delete($commentid);
    }
    
    /**
     * Update message
     * 
     * @param int $msgid Message ID
     * @param string $name New user name
     * @param string $text New message text
     * @author Kirill Krasnoschekov 
     */
    public function UpdateMessage($msgid, $name, $text)
    {
        global $DOF;
        
        // Forming the object
        $message = new stdClass;
        $message->id = $msgid;
        $message->name = $name;
        $message->text = $text;
        $message->timestamp = time();
        // Querying the database
        return $DOF->storage('gbmessages')->update($message);
    }

    /**
     * MessageExists
     * 
     * @param int $msgid Message ID
     * @return True if yes, False if no
     * @author Kirill Krasnoschekov 
     */    
    protected function MessageExists($msgid)
    {
        global $DOF;
        return $DOF->storage('gbmessages')->is_exists($msgid);
    }
    
    /**
     * CommentExists
     * 
     * @param int $commentid Comment ID
     * @return True if yes, False if no
     * @author Kirill Krasnoschekov 
     */    
    protected function CommentExists($commentid) 
    {
        global $DOF;
        return $DOF->storage('gbcomments')->is_exists($commentid);
    }
}

?>